package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.WorkExperienceData;

public class WorkListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<WorkExperienceData> mList;

	public WorkListAdapter(Context context) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setData(List<WorkExperienceData> list) {
		mList = list;
	}

	public void addData(List<WorkExperienceData> list) {
		if (null == mList) {
			mList = new ArrayList<WorkExperienceData>();
		}
		for (int i = 0; i < list.size(); i++) {
			mList.add(list.get(i));
		}
	}

	public void clear() {
		mList.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.work_list_item, null);   
			holder = new GridHolder();
			holder.title = convertView.findViewById(R.id.work_title);
			holder.time = convertView.findViewById(R.id.working_time);
			holder.post = convertView.findViewById(R.id.work_post);
			holder.desc = convertView.findViewById(R.id.work_content);
			holder.close = convertView.findViewById(R.id.work_item_close);
			convertView.setTag(holder); 
		}else{
			holder = (GridHolder) convertView.getTag();
		}
		WorkExperienceData data = mList.get(position);
		if(null != data){
			holder.title.setText("工作经历" + position);
			holder.time.setText(data.getStartDate() + " 至 " + data.getEndDate());
			holder.post.setText(data.getPost());
			holder.desc.setText(data.getContent());
			holder.close.setOnClickListener(new MyListener(position));
		}
		return convertView;
	}
	
	public class GridHolder {
		public TextView title;
		public TextView time;
		public TextView post;
		public TextView desc;
		public Button close;
	}
	
	class MyListener implements OnClickListener{

		private int position;
		
		public MyListener(int position) {
			this.position = position;
		}
		
		@Override
		public void onClick(View v) {
			mList.remove(position);
			notifyDataSetChanged();
		}
		
	}

}
